﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using MySql.Data.MySqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TJ_MainNameSpace
{
    public partial class Form_k40 : Form
    {
        private kongjian40.liangxiangdianliu mydianliu;
        public Form_k40(kongjian40.liangxiangdianliu id)
        {
            InitializeComponent();
            this.mydianliu = id;
        }
        private bool loading = false;
        private ZTName _ctr_kind = ZTName.ZTNameCount;
        MySqlConnection con = new MySqlConnection(TongJi_EMS.Properties.Settings.Default.monitorConnection);

        private void Form1_Load(object sender, EventArgs e)
        {
            loading = true;
            this.tB_Name.Text = mydianliu.Site.Name;
            this.textBox2.Text = mydianliu.FaultSignal;
            this.textBox3.Text = mydianliu.Dianliu_ia;
            this.textBox4.Text = mydianliu.Dianliu_ib;
            this.textBox5.Text = mydianliu.Dianliu_ic;
            try
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                string sql1 = "select id, description from dbarea where project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";

                MySqlDataAdapter oda1 = new MySqlDataAdapter(sql1, con);

                DataSet ds3 = new DataSet();

                oda1.Fill(ds3, "dbarea");

                this.cBx_Area.DataSource = ds3.Tables[0];
                this.cBx_Area.DisplayMember = "description";
                this.cBx_Area.ValueMember = "id";
                con.Close();
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
            loading = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _ctr_kind = ZTName.kj40;
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            string mysql = "SELECT  ctr_name  FROM  run_ctr where ctr_name='" + mydianliu.Name + "'AND project='"
                + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'AND subpage='" + TongJi_EMS._Management.ProjectManager.Instance.CurSubPage + "'";
            //string mysql = "SELECT   COUNT( txt_name='" + mytext.Name + "') FROM  run_temp ";

            MySqlCommand cmd = new MySqlCommand(mysql, con);
            object count = cmd.ExecuteScalar();

            string sql;

            if (count == null)

                sql = "INSERT INTO run_ctr(ctr_name,ctr_kind,project,db_pointid,subpage) VALUES('"
                    + tB_Name.Text + "' ," + Convert.ToInt32(_ctr_kind) + ",'"
                    + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'," + (int)cBx_Ia.SelectedValue + ",'"
                    + TongJi_EMS._Management.ProjectManager.Instance.CurSubPage + "')";
            //sql = "INSERT INTO run_ctr(ctr_name,db_name,ctr_kind) VALUES( '" + textBox1.Text + "' ,'" + kind + "','" + kind + "')";
            else
                sql = "update run_ctr set db_pointid='" + (int)cBx_Ia.SelectedValue + "' where ctr_name='"
                    + tB_Name.Text + "'AND project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'AND subpage='"
                    + TongJi_EMS._Management.ProjectManager.Instance.CurSubPage + "'";

            MySqlCommand mInsertCmd = new MySqlCommand(sql, con);
            mInsertCmd.CommandType = CommandType.Text;
            mInsertCmd.ExecuteNonQuery();
            con.Close();
            mydianliu.Name = this.tB_Name.Text;
            mydianliu.FaultSignal = this.textBox2.Text;
            mydianliu.Dianliu_ia = this.textBox3.Text;
            mydianliu.Dianliu_ib = this.textBox4.Text;
            mydianliu.Dianliu_ic = this.textBox5.Text;
            this.mydianliu.Refresh();
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loading) return;
            fresh();
        }

        private void fresh()
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            string sql = "select * from dbpoint where project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'AND db_areaid='" + (int)cBx_Area.SelectedValue + "'";
            MySqlDataAdapter oda = new MySqlDataAdapter(sql, con);

            DataSet ds = new DataSet();
            ds.Clear();
            oda.Fill(ds, "dbpoint");
            this.cBx_Ia.DataSource = ds.Tables["dbpoint"];
            this.cBx_Ia.DisplayMember = "db_description";
            this.cBx_Ia.ValueMember = "id";
            this.cBx_Ib.DataSource = ds.Tables["dbpoint"];
            this.cBx_Ib.DisplayMember = "db_description";
            this.cBx_Ib.ValueMember = "id";
            this.cBx_Ic.DataSource = ds.Tables["dbpoint"];
            this.cBx_Ic.DisplayMember = "db_description";
            this.cBx_Ic.ValueMember = "id";
            this.cBx_Fault.DataSource = ds.Tables["dbpoint"];
            this.cBx_Fault.DisplayMember = "db_description";
            this.cBx_Fault.ValueMember = "id";
            con.Close();
        }
    }
}